//
//  UIButton+EL.m
//  JTProject
//
//  Created by Yuki on 2021/3/9.
//

#import "UIButton+EL.h"
#import <objc/runtime.h>
#import <UIButton+WebCache.h>
#import "UIImage+EL.h"
@implementation UIButton (EL)
/// 创建按钮
/// @param titleStr 标题
/// @param titleFont 标题字体
/// @param titleColor 标题颜色
+ (UIButton *)el_createButtonWithTitle:(NSString *)titleStr
                             titleFont:(UIFont *)titleFont
                            titleColor:(UIColor *)titleColor{
    return [self _createBtnWithTitle:titleStr titleFont:titleFont titleColor:titleColor traget:nil action:nil actionBlock:nil];
}

/// 创建按钮
/// @param titleStr 标题
/// @param titleFont 标题字体
/// @param titleColor 标题颜色
/// @param traget 消息的执行者
/// @param action 点击事件
+ (UIButton *)el_createButtonWithTitle:(NSString *)titleStr
                             titleFont:(UIFont *)titleFont
                            titleColor:(UIColor *)titleColor
                                traget:(id)traget
                                action:(SEL)action{
    return [self _createBtnWithTitle:titleStr titleFont:titleFont titleColor:titleColor traget:traget action:action actionBlock:nil];
}

/// 创建按钮
/// @param titleStr 标题
/// @param titleFont 标题字体
/// @param titleColor 标题颜色
/// @param actionBlock 标题点击事件回调
+ (UIButton *)el_createButtonWithTitle:(NSString *)titleStr
                             titleFont:(UIFont *)titleFont
                            titleColor:(UIColor *)titleColor
                           actionBlock:(EL_ClickCurrenBtnBlock)actionBlock{
    
    return [self _createBtnWithTitle:titleStr titleFont:titleFont titleColor:titleColor traget:nil action:nil actionBlock:actionBlock];
}


///private 私有方法
+ (UIButton *)_createBtnWithTitle:(NSString *)titleStr
                        titleFont:(UIFont *)titleFont
                       titleColor:(UIColor *)titleColor
                           traget:(id)traget
                           action:(SEL)action
                      actionBlock:(EL_ClickCurrenBtnBlock)actionBlock{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = UIColor.clearColor;
    [btn setTitle:titleStr forState:UIControlStateNormal];
    btn.titleLabel.font = titleFont;
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    if (actionBlock){
        btn.clickCurrentBtnBlock = actionBlock;
    }
    if (action){
        ///消息执行者
        [btn addTarget:traget action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
    
}


/**  动态添加属性  **/
///set方法
- (void)setClickCurrentBtnBlock:(EL_ClickCurrenBtnBlock)clickCurrentBtnBlock{
    objc_setAssociatedObject(self, @selector(clickCurrentBtnBlock), clickCurrentBtnBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    [self addTarget:self action:@selector(clickCurrentBtn) forControlEvents:UIControlEventTouchUpInside];
}
///get方法
- (EL_ClickCurrenBtnBlock)clickCurrentBtnBlock{
    return objc_getAssociatedObject(self, _cmd);
}

///点击事件
- (void)clickCurrentBtn{
    EL_ClickCurrenBtnBlock block = (EL_ClickCurrenBtnBlock )objc_getAssociatedObject(self, @selector(clickCurrentBtnBlock));
    if (block) {
        block(self);
    }
}



#pragma mark - --------- 设置网络图片  ---------
- (void)sd_setFadeImageWithURL:(NSURL *)url forState:(UIControlState)state  placeholderImage:(UIImage *)placeholder
{
    __weak __typeof(self)wself = self;
    [self sd_setImageWithURL:url forState:state placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            CATransition *animation = [CATransition animation];
            [animation setDuration:0.65f];
            [animation setType:kCATransitionFade];
            animation.removedOnCompletion = YES;
            [wself.layer addAnimation:animation forKey:@"transition"];
        }
    }];
    [self.layer removeAnimationForKey:@"transition"];
}

- (void)sd_setFadeImageWithURL:(NSURL *)url forState:(UIControlState)state
{
    __weak __typeof(self)wself = self;
    if (@available(iOS 13.0, *)) {
        [self sd_setImageWithURL:url forState:state placeholderImage:[UIImage imageWithColor:[UIColor placeholderTextColor]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                CATransition *animation = [CATransition animation];
                [animation setDuration:0.65f];
                [animation setType:kCATransitionFade];
                animation.removedOnCompletion = YES;
                [wself.layer addAnimation:animation forKey:@"transition"];
            }
        }];
    } else {
        [self sd_setImageWithURL:url forState:state placeholderImage:[UIImage imageWithColor:UIColor.lightTextColor] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                CATransition *animation = [CATransition animation];
                [animation setDuration:0.65f];
                [animation setType:kCATransitionFade];
                animation.removedOnCompletion = YES;
                [wself.layer addAnimation:animation forKey:@"transition"];
            }
        }];
    }
    [self.layer removeAnimationForKey:@"transition"];
}
@end
